import { Component, Host, ChangeDetectionStrategy, ChangeDetectorRef, OnInit } from '@angular/core';
import { AppComponent } from '../../../../../app.component';
import { CompanyService } from '../../company.service';
import { CompanyModel } from '../../company.model';
import { CompanyGlobalCompactService, GCAssesment } from '../companyGlobalCompact.service';
import { CompanyGCSummaryModel } from '../models/companyGCsummary.model';
import { CompanyGCPrincipleModel } from '../models/companyGCprinciple.model';
import { AnalyticsService } from '../../../../../shared/services/googleAnalytics.service';
import { GoogleAnalyticsBeacon } from '../../../../../shared/models/googleAnalytics.model';
import { Subscription } from 'rxjs/Subscription';

@Component({
    moduleId: module.id,
    templateUrl: 'global-compact-overview.component.html',
    styleUrls: ['../companyGlobalCompact.component.scss'],
    changeDetection: ChangeDetectionStrategy.OnPush
})
export class GlobalCompactOverviewComponent implements OnInit {
    public company: CompanyModel;
    public gcSummary: CompanyGCSummaryModel;
    public gcPrinciples: CompanyGCPrincipleModel[];
    public gCAssessment: typeof GCAssesment = GCAssesment;
    public pillars = [];
    public hasNonCompliant: boolean;
    public hasWatchlist: boolean;
    public hasCompliant: boolean;

    constructor(
        private cdRef: ChangeDetectorRef,
        public companyService: CompanyService,
        public companyGlobalCompactService: CompanyGlobalCompactService,
        private analyticsService: AnalyticsService,
        @Host() private rootComponent: AppComponent
    ) {
        this.pillars = companyGlobalCompactService.getPillars();
    }

    ngOnInit() {
        this.getData();
    }

    public getData() {
        this.companyService.company.subscribe(company => {
            this.company = company;
            this.analyticsService.sendBeacon(new GoogleAnalyticsBeacon('OnscreenGlobalCompactReport', 'Research', { CompanyId: this.company.id }));
            this.companyService.setHeaderData(undefined);

            this.companyGlobalCompactService.getByCompany(this.company.id).subscribe(summary => {
                if(summary == null) {
                    this.gcSummary = new CompanyGCSummaryModel(company.country);
                }
                else {
                    this.gcSummary = summary ;
                    this.companyGlobalCompactService.getOverallPricipleData(this.company.id);
                    if ( this.gcSummary.overallCompanyAssessment !== GCAssesment.Compliant) {
                          this.companyGlobalCompactService.principleData.subscribe(results => {
                            // set permissions to each list
                            this.hasNonCompliant = results[0] !== false;
                            const nonCompliantData = [].concat(results[0] || []);
                            this.hasWatchlist = results[1] !== false;
                            const watchlistData = [].concat(results[1] || []);
                            // combine lists
                            this.gcPrinciples = [].concat(nonCompliantData, watchlistData);
                            this.cdRef.markForCheck();
                        });
                    }
                }
                this.cdRef.markForCheck();
                this.rootComponent.finishLoading();     
            });
        });
    }

    public getPillar(pillarId: number): CompanyGCPrincipleModel[] {
        return this.gcPrinciples.filter(x => x.principle.pillar.id === pillarId);
    }
}
